package com.yuerenjf.yuerenjinfu.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.activity.MainActivity;
import com.yuerenjf.yuerenjinfu.adapter.FirstEvent;
import com.yuerenjf.yuerenjinfu.bean.ProjectTypeBean;
import com.yuerenjf.yuerenjinfu.customview.SegmentControl;
import com.yuerenjf.yuerenjinfu.http.Constants;
import com.yuerenjf.yuerenjinfu.http.Xutils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by kiddo on 16-9-20.
 */

public class FragmentLendList extends BaseFragment {
    View view;
    String []typeName;
    private SegmentControl mSegmentHorzontal;
    public Fragment[] mTabFragment ;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;

    public Fragment getmCurrentFragment() {
        return mCurrentFragment;
    }

    public void setmCurrentFragment(Fragment mCurrentFragment) {
        this.mCurrentFragment = mCurrentFragment;
    }

    Fragment mCurrentFragment;
    FrameLayout frameLayout;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.i("FragmentHaiTao_onCreate");
        if (!EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//反注册EventBus
    }

    @Override
    public void prepareData() {
        LogUtil.i("FragmentHaiTao_prepareData");
        mFragmentManager = getChildFragmentManager();
        mTransaction = mFragmentManager.beginTransaction();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(FirstEvent event) {

        String msg = "onEventMainThread收到了消息：" + event.getMsg();
        LogUtil.i(msg);
        if(event.getMsg()!=null){
            selectFragment(0);
        }

    }
    @Override
    public View prepareView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_haitao, null);
        LogUtil.i("FragmentHaiTao:prepareView");
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtil.i("FragmentHaiTao:onActivityCreated");
        MainActivity mainActivity= (MainActivity) getActivity();
        mainActivity.setmTitleTextView("项目列表");
        mainActivity.setLogoImageShow(false);
    }

    @Override
    public void onloadData(View view) {
        LogUtil.i("FragmentHaiTao:onloadData");
        frameLayout= (FrameLayout) view.findViewById(R.id.content_list);
        mSegmentHorzontal = (SegmentControl) view.findViewById(R.id.segment_control);
        mSegmentHorzontal.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                Log.i("click", "onSegmentControlClick: index = " + index);
                switchContent(mCurrentFragment, mTabFragment[index], index);
            }
        });

        getRemoteForData();
    }

    @Override
    public void adapterScreen() {

    }


    public void selectFragment(int index){
        if(mSegmentHorzontal!=null){
            mSegmentHorzontal.setSelectedIndex(0);
        }


    }

    /**
     * fragment 切换
     *
     * @param from
     * @param to
     */
    public void switchContent(Fragment from, Fragment to, int position) {
        if (mCurrentFragment != to) {
            mCurrentFragment = to;
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(from)
                        .add(R.id.content_list, to, typeName[position]).commitAllowingStateLoss ()        ; // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commitAllowingStateLoss (); // 隐藏当前的fragment，显示下一个
            }
        }
    }


    private void getRemoteForData(){
        Map<String, String> map = new HashMap<>();
        Xutils.getInstance().post(getActivity(), Constants.URL_PROJECT_TYPE, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                LogUtil.i(result);
                ProjectTypeBean mTypeBean = gson.fromJson(result, ProjectTypeBean.class);
                typeName =new String[mTypeBean.getData().size()];
                mTabFragment=new Fragment[typeName.length];
                for(int i=0;i<mTypeBean.getData().size();i++){
                   ProjectTypeBean.DataBean bean= mTypeBean.getData().get(i);
                    typeName[i]=bean.getName();
                    if(bean.isTransfer()){
                        mTabFragment[i] = FragmentLendOther.getInstance(bean.getType()+"");
                    }else{
                        mTabFragment[i] = FragmentLendYouXuan.getInstance(bean.getType()+"");
                    }
                }
                mSegmentHorzontal.setText(typeName);
                mCurrentFragment=mTabFragment[0];
                mTransaction.add(R.id.content_list, mTabFragment[0], typeName[0]).commitAllowingStateLoss () ;

//                EventBus.getDefault().post(
//                        new FirstEvent("FirstEvent btn clicked"));
            }

            @Override
            public void onFailer(int status, String result){

            }

            @Override
            public void onFinish() {

            }
        });
    }
}
